package com.xxxt.jxj.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxt.entity.ClassGrade;
import com.xxxt.entity.College;
import com.xxxt.entity.Grade;
import com.xxxt.entity.Specialty;
import com.xxxt.entity.Student;
import com.xxxt.jxj.service.ClassGradeService;
import com.xxxt.jxj.service.CollegeService;
import com.xxxt.jxj.service.GradeService;
import com.xxxt.jxj.service.SpecialtyService;
import com.xxxt.utils.UUIDUtil;

@Controller
@RequestMapping("/specialty")
public class SpecialtyController {
	@Resource
	private CollegeService collegeService;
	@Resource
	private SpecialtyService specialtyService;
	@Resource
	private GradeService gradeService;
	@Resource
	private ClassGradeService classGradeService;
	
	/***
	 * 插入专业
	 * @param schoolId
	 * @param collegeId
	 * @param collegeName
	 * @return
	 */
	@RequestMapping(value="/insertSpecialty",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject insertSpecialty(String collegeId,String specialtyName){
		JSONObject json = new JSONObject();
		try {
			College college = collegeService.selcetOneCollegeByCollegeId(collegeId);
			if (college==null) {
				json.put("status", 2);
				json.put("message", "当前学院不存在,添加失败");
				return json;
			}				
			List<Specialty> list = specialtyService.selcetAllSpecialtyByCollegeId(collegeId);//学院对应的学校下的遍历
			Specialty specialty = new Specialty();
			specialty.setSpecialtyId(UUIDUtil.getUUID());
			specialty.setSpecialtyName(specialtyName);
			specialty.setCollege(college);
			for (Specialty specialty2 : list) {			
				if (specialty2.getSpecialtyName()==specialtyName||specialty2.getSpecialtyName().equals(specialtyName)) {
					json.put("status", -2);
					json.put("message", "当前学院已有相同的专业名称存在，请不要重复添加！");
					return json;			
				}
			}
			boolean insertSpecialty = specialtyService.insertSpecialty(specialty);
			if (insertSpecialty) {
				json.put("status", 0);
				json.put("message", "添加成功");
			}
			else {
				json.put("status", 1);
				json.put("message", "添加失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "添加失败");
			return json;
		}		
		return json;		
	}
	/**'
	 * 查询单个专业信息
	 * @param collegeId
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/selcetOneSpecialtyBySpecialtyId",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selcetOneSpecialtyBySpecialtyId(String specialtyId,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			Specialty selcetOneSpecialtyBySpecialtyId = specialtyService.selcetOneSpecialtyBySpecialtyId(specialtyId);
			if (selcetOneSpecialtyBySpecialtyId!=null) {
				session.setAttribute("selcetOneSpecialtyBySpecialtyId", selcetOneSpecialtyBySpecialtyId);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("speciality", selcetOneSpecialtyBySpecialtyId);			
			}else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;
		}	
		return json;				
	}
	/**
	 * 修改单个专业信息
	 * @param collegeId
	 * @param schoolId
	 * @param collegeName
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/updateSpecialty",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject updateSpecialty(String collegeId,String specialtyName,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			Specialty specialty=(Specialty) session.getAttribute("selcetOneSpecialtyBySpecialtyId");


			College college = collegeService.selcetOneCollegeByCollegeId(collegeId);
			if (college==null) {
				json.put("status", -1);
				json.put("message", "抱歉，当前列表中没有该学院！");
				return json;
			}
			List<Specialty> selcetAllSpecialtyByCollegeId = specialtyService.selcetAllSpecialtyByCollegeId(collegeId);
			for (Specialty specialty2 : selcetAllSpecialtyByCollegeId) {				
				if (specialty2.getSpecialtyName()==specialtyName||specialty2.getSpecialtyName().equals(specialtyName)) {
					json.put("status", -3);
					json.put("message", "抱歉，该学院已经有相同的专业名称咯！");
					return json;
				}
			}
	
			boolean updateSpecialty = specialtyService.updateSpecialty(specialtyName, college.getCollegeId(), specialty.getSpecialtyId());
			if (updateSpecialty) {
				json.put("status", 0);
				json.put("message", "修改成功");
			}
			else {
				json.put("status", 1);
				json.put("message", "修改失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "修改失败");
			return json;
		}		
		return json;
		}	
	/**
	 * 删除单个专业信息
	 * @param collegeId
	 * @return
	 */
	@RequestMapping(value="/deleteSpecialty",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject deleteSpecialty(String specialtyId){
		JSONObject json = new JSONObject();
		try {
			boolean deleteSpecialty = specialtyService.deleteSpecialty(specialtyId);
			if (deleteSpecialty) {
				json.put("status", 0);
				json.put("message", "删除成功");
			}
			else {
				json.put("status", -1);
				json.put("message", "删除失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "删除失败");
			return json;
		}		
		return json;
	}
	/**
	 * 根据学院id 分页查询专业信息
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selcetAllPageSpecialtyByCollegeId",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selcetAllPageSpecialtyByCollegeId(String collegeId,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<Specialty> selcetAllPageSpecialtyByCollegeId = specialtyService.selcetAllPageSpecialtyByCollegeId(collegeId, pageNum, pageSize);
			if (selcetAllPageSpecialtyByCollegeId.size()>0) {
				PageInfo<Specialty> pageInfo=new PageInfo<>(selcetAllPageSpecialtyByCollegeId);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("pageInfo", pageInfo);
			}
			else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;
		}		
		return json;
	}
	/**
	 * 根据老师和专业查询出对应学生分页
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selectStudentByTnoAndSpecialty",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selectStudentByTnoAndSpecialty(String specialtyId,String tno,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {	
			
			List<Grade> selcetAllGradeBySpecialtyId = gradeService.selcetAllGradeBySpecialtyId(specialtyId);
			
			List<ClassGrade> selectStudentByTnoAndSpecialty = gradeService.selectStudentByTnoAndSpecialty(selcetAllGradeBySpecialtyId);	
			
			if (selcetAllGradeBySpecialtyId.size()<=0) {
				json.put("status", -4);
				json.put("message", "当前对应年级没有你的学生");	
				return json;
			}
			if (selectStudentByTnoAndSpecialty.size()<=0) {
				json.put("status", -3);
				json.put("message", "当前对应班级没有你的学生");
				return json;
			}
			PageHelper.startPage(pageNum, pageSize);
			List<Student> selectStudentByTnoAndList = classGradeService.selectStudentByTnoAndList(tno, selectStudentByTnoAndSpecialty,pageNum,pageSize);
			if (selectStudentByTnoAndList.size()>0) {
				PageInfo<Student> pageInfo=new PageInfo<>(selectStudentByTnoAndList);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("pageInfo", pageInfo);
			}
			else if (selectStudentByTnoAndList.size()==0) {
				json.put("status", -2);
				json.put("message", "当前没数据");
			}
			else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
		}
		return json;
	}
}
